package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WechatServiceImpl implements WechatService {
     private static final String OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";
     private static final String PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={}";
     private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.appSecret}")
    private String appSecret;
    @Override
    public String getOpenId(String code){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret",appSecret );
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");

        HttpResponse res = HttpRequest.get(OPEN_ID_URL).form(paramMap).timeout(5000).execute();
        if(res.isOk()){
            String body = res.body();
             //解析
            JSONObject entries = JSONUtil.parseObj(body);
             String openid = entries.getStr("openid");
             if ( openid != null){
              return openid;}
        }

        return null;
    }

    @Override
    //获取电话号码
    public String getPhoneNumber(String phoneCode){

        //调用getAccessToken方法获取access_token
        String access_token = getAccessToken();
        System.out.println("access_token = " + access_token);
        String url = StrUtil.format(PHONE_NUMBER_URL, access_token);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", phoneCode);
        HttpResponse execute = HttpRequest.post(url).body(JSONUtil.toJsonStr( paramMap)).timeout(5000).execute();
        if ( execute.isOk()){
            String body = execute.body();
            //解析
            JSONObject entries = JSONUtil.parseObj(body);
            //获取属性
            String phone = entries.getByPath("phone_info.phoneNumber").toString();
            return phone;
        }
        return  null;
    }
    private String  getAccessToken() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type", "client_credential");
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);

        //发送请求
        HttpResponse execute = HttpRequest.get(ACCESS_TOKEN_URL).form(paramMap).timeout(5000).execute();
        if ( execute.isOk()){
            //解析json对象
            String result = execute.body();
            JSONObject entries = JSONUtil.parseObj(result);
            String accessToken = entries.getStr("access_token");

            System.out.println( "accessToken = " + accessToken);
            return accessToken;
        }
        return null;
    }
}
